package com.suyang.syt.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.model.UserMessage;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.MessageDTO;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.UserMessageService;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/admin/user/message")
@AuthClass
public class AdminUserMessageController {

	@Autowired
	private UserMessageService userMessageService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(String userId, Model model) {
		if (EmptyUtil.isNotEmpty(userId)) {
			model.addAttribute("userId", userId);
		}
		return "user/message/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<MessageDTO> list(HttpServletRequest request, int page, int rows, String sort, String order, UserMessage userMessage) {
		try {
			String createDate1 = request.getParameter("createDate1");
			String createDate2 = request.getParameter("createDate2");
			return userMessageService.listByEasyUI(page, rows, sort, order, createDate1, createDate2, userMessage);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj delete(HttpServletRequest request, String ids, String userId) {
		try {
			return userMessageService.delete(ids, Long.parseLong(userId));
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, String ids, String userId) {
		try {
			return userMessageService.update(Long.parseLong(userId), ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}
}
